#include "compile_run.hpp"
#include "../comm/httplib.h"

// ./compile_server port
int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        std::cout << "please run by ./compile_server port" << std::endl;
        exit(1);
    }

    httplib::Server svr;

    svr.Get("/hello", [](const httplib::Request& req, httplib::Response& resp)
    {
        resp.set_content("hello wyq, 你好httplib!", "text/plain;charset=utf-8");
    });

    svr.Post("/compile_and_run", [](const httplib::Request& req, httplib::Response& resp)
    {
        std::string json_in = req.body; // 客户端提交的源代码
        std::string json_out; // 返回运行结果
        if(!json_in.empty())
        {
            ns_compile_and_run::Compile_And_Run::Start(json_in, json_out);
            resp.set_content(json_out, "application/json;charset=utf-8");
        }
    });

    int port = std::atoi(argv[1]);
    svr.listen("0.0.0.0", port);
    // Json::Value json_value;
    // json_value["code"] = 
    // R"(#include <iostream>
    // int main()
    // {
    //     std::cout << "hello json" << std::endl;
    //     return 0;
    // })";
    // json_value["input"] = "";
    // json_value["cpu_limit"] = 1;
    // json_value["mem_limit"] = 1024 * 20;

    // Json::StyledWriter writer;
    // std::string json_str = writer.write(json_value);

    // // std::cout << json_str << std::endl;
    // std::string out_json;
    // ns_compile_and_run::Compile_And_Run::Start(json_str, out_json);
    // std::cout << out_json << std::endl;

    return 0;
}